<?php
/**
 * 查询快递员信息
 * Created by PhpStorm.
 * User: 赵沛龙
 * Date: 2017/3/21
 * Time: 17:03
 */
namespace soa\wduser;

use artisan\route;
use soa\client;
use artisan\cache;
use soa\common\Remote;
use soa\remind\Remind;
use util\test;

class Wduser extends client{
    private $cache;
    private $key = 'WduserInfo:Wduser';
    private $hashKey = '';


    function __construct(){
        $this->cache = cache::connect('base');
    }
    /**
     * 获取快递员信息
     * 功能：查询快递员信息
     * @param $id  用户id
     * @param $select  是否查库  默认先查缓存    true 查库
     * @return array();
     */
    public function  getWduserInfo($id,$select = false){
        //公共必传参数
        if(!isset($id) || empty($id)){return false;}  //用户id
        try{
            if($select === false){
                $this->hashKey = 'Wduser:'.$id;   //hash键
                $wduserInfo = $this->cache->hget($this->key,$this->hashKey);
            }
            if(!$wduserInfo || $select ===true){
                $wduserInfo = self::addWduser($id);
            }
            return $wduserInfo;
        } catch (\RedisException $e) {
            logError($e->getMessage(), 'getWduserInfo not fail');
        }
    }

    /**
     * 获取快递员信息
     * 功能：查询快递员信息
     * @param $id  用户id
     * @param $select  是否查库  默认先查缓存    true 查库
     * @return array();
     */
    public function  getWduserMobileInfo($mobile,$select = false){
        //公共必传参数
        if(!isset($mobile) || empty($mobile)){return false;}  //用户id
        try{
            $this->hashKey = 'Wduser:'.$mobile;   //hash键
            $wduserInfo = $this->cache->hget($this->key,$this->hashKey);
            if(!$wduserInfo || $select ===true){
                $wduserInfo = self::addWduserMobile($mobile);
                return $wduserInfo;
            }
            $wduserId = json_decode($wduserInfo,true);
            $info = $this->getWduserInfo($wduserId);
            return $info;
        } catch (\RedisException $e) {
            logError($e->getMessage(), 'getWduserInfo not fail');
        }
    }
    /**
     * 更新快递员信息
     * $id 快递员ID
     **/
    public function  updateWduserInfo($id){
        if(!isset($id) ||empty($id)){return false;}
        try{
            $this->hashKey = 'Wduser:'.$id;
            $wduserInfo = $this->cache->hget($this->key,$this->hashKey);
            if($wduserInfo){

            }else{
                $this->getWduserInfo($id);
            }
        }catch(\RedisException $e){
            logError($e->getMessage(), 'updateWduserInfo not fail');
        }
    }
    /**
     * 添加快递员信息
     * $id 快递员ID
     **/
    public function addWduser($id){
        $remote = new Remote('account.core');
        if($id){
            $wduserInfo = $remote->doPost('v1/courier/info', array('courier_id'=> $id));
            $wduser = $wduserInfo;
            $this->hashKey = 'Wduser:'.$id;   //hash键
            $wduserInfo = json_encode($wduserInfo);
            $this->cache->hset($this->key,$this->hashKey,$wduserInfo);
            $this->cache->expire($this->key.$this->hashKey,"90000"); //缓存设置25H
            $this->cache->hset($this->key,'Wduser:'.$wduser['username'],json_encode($id));   //手机号缓存时间设置为 一周
            $this->cache->expire($this->key.'Wduser:'.$wduser['username'],"604800"); //缓存设置25H
            unset($wduser,$id);
            return $wduserInfo;
        }
        return false;

    }
    /**
     * 添加快递员信息
     * $mobile 快递员手机号
     **/
    public function addWduserMobile($mobile){
        $remote = new Remote('account.core');
        if($mobile){
            $wduserInfo = $remote->doPost('v1/courier/info', array('username'=> $mobile));
            $wduser = $wduserInfo;
            $this->hashKey = 'Wduser:'.$mobile;   //hash键
            $wduserInfo = json_encode($wduserInfo);
            $this->cache->hset($this->key,$this->hashKey,json_encode($wduser['id']));
            $this->cache->expire($this->key.$this->hashKey,"604800"); //缓存设置一周
            $this->cache->hset($this->key, 'Wduser:'.$wduser['id'],$wduserInfo);
            $this->cache->expire($this->key.'Wduser:'.$wduser['id'],"90000"); //缓存设置25H
            unset($wduser,$mobile);
            return $wduserInfo;
        }else{
            return false;
        }
    }
    /**
     * 删除快递员信息
     **/
    public function delWduser($id){
        if(!isset($id) ||empty($id)){return false;}
        $this->hashKey = 'Wduser:'.$id;
//        $wduserInfo = $this->cache->hget($this->key,$this->hashKey);
        $wduserInfo = $this->cache->hDel($this->key,$this->hashKey);
        if($wduserInfo){  //当前用户无消息
            return 'ok';
        }else{
            return "false";
        }
    }
    /**
     * 获取快递员最新的一次地理位置信息
     **/
    public function  getUserNewLocation($where){
        if(!$where['username']){
            return false;
        }
        $remote = new Remote('account.core');//kuaidihelp_user
        $wduserMap = $remote->doPost('v1/courier/UserNewLocation', $where);
        return $wduserMap;
    }
}